package com.rbac.admin.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rbac.admin.common.Result;
import com.rbac.admin.entity.SysPermission;
import com.rbac.admin.service.SysPermissionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.tags.Tags;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 权限控制器
 */
@RestController
@RequestMapping("/api/permissions")
@Tag(name = "权限管理", description = "权限管理")
public class PermissionController {

    @Autowired
    private SysPermissionService permissionService;

    /**
     * 分页查询权限列表
     * @param pageNum 页码
     * @param pageSize 每页数量
     * @param keyword 关键词
     * @return 分页结果
     */
    @Operation(summary = "分页查询权限列表")
    @GetMapping("/page")
    @PreAuthorize("hasAuthority('sys:permission:list')")
    public Result<Page<SysPermission>> listPermissionPage(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String keyword) {
        Page<SysPermission> page = permissionService.listPermissionPage(pageNum, pageSize, keyword);
        return Result.success(page);
    }

    /**
     * 获取所有权限列表
     * @return 权限列表
     */
    @Operation(summary = "获取所有权限列表")
    @GetMapping("/list")
    @PreAuthorize("hasAuthority('sys:permission:list')")
    public Result<List<SysPermission>> listAllPermissions() {
        List<SysPermission> permissions = permissionService.listAllPermissions();
        return Result.success(permissions);
    }

    /**
     * 获取权限详情
     * @param id 权限ID
     * @return 权限详情
     */
    @Operation(summary = "获取权限详情")
    @GetMapping("/{id}")
    @PreAuthorize("hasAuthority('sys:permission:list')")
    public Result<SysPermission> getPermissionById(@PathVariable Long id) {
        SysPermission permission = permissionService.getById(id);
        return Result.success(permission);
    }

    /**
     * 添加权限
     * @param permission 权限信息
     * @return 添加结果
     */
    @Operation(summary = "添加权限")
    @PostMapping
    @PreAuthorize("hasAuthority('sys:permission:add')")
    public Result<Boolean> addPermission(@Validated @RequestBody SysPermission permission) {
        boolean result = permissionService.save(permission);
        return Result.success(result);
    }

    /**
     * 更新权限
     * @param id 权限ID
     * @param permission 权限信息
     * @return 更新结果
     */
    @Operation(summary = "更新权限")
    @PutMapping("/{id}")
    @PreAuthorize("hasAuthority('sys:permission:update')")
    public Result<Boolean> updatePermission(@PathVariable Long id, @Validated @RequestBody SysPermission permission) {
        permission.setId(id);
        boolean result = permissionService.updateById(permission);
        return Result.success(result);
    }

    /**
     * 删除权限
     * @param id 权限ID
     * @return 删除结果
     */
    @Operation(summary = "删除权限")
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('sys:permission:delete')")
    public Result<Boolean> deletePermission(@PathVariable Long id) {
        boolean result = permissionService.removeById(id);
        return Result.success(result);
    }

    /**
     * 根据用户ID获取权限列表
     * @param userId 用户ID
     * @return 权限列表
     */
    @Operation(summary = "根据用户ID获取权限列表")
    @GetMapping("/user/{userId}")
    @PreAuthorize("hasAuthority('sys:permission:list')")
    public Result<List<SysPermission>> getPermissionsByUserId(@PathVariable Long userId) {
        List<SysPermission> permissions = permissionService.getPermissionsByUserId(userId);
        return Result.success(permissions);
    }
}